// 引入文件
// 公用样式初始样式
require("../../assets/css/index.less");
require("./train.less");
require("../../assets/fonts/iconfont.css");

// 引入工具函数
const dom = require('../../utils/dom')
const https = require('../../utils/https');
const local = require('../../utils/local')

dom.ready(function() {
    dom.hMenu('train')
    dom.bMenu('motion')

    https.get('/api/train/courseList', function(res) {
        // console.log(res);
        let { errno, data } = res
        if (errno == 0) {
            let newData = data.filter(v => v.courseId == 1)
            let cData = data.filter(v => v.courseId > 1)
                // console.log(newData);
                // let strHtml = ''
                // newData.map(function(v) {
                //     return strHtml += `<li>
                //     <img src="http://fitness.h5.itsource.cn:3701${v.imgurl}" alt="">
                //     <div class="title">
                //       <h5>${v.name}</h5>
                //       <p>${v.desc}</p>
                //     </div>
                // </li>`
                // })
                // dom.getEle('.new').innerHTML = strHtml
            rander(newData, '.new')
            rander(cData, '.course')
        }
    })

    function rander(dt, obj) {
        let strHtml = ''
        dt.map(function(v) {
            return strHtml += `<li>
           <img src="http://fitness.h5.itsource.cn:3701${v.imgurl}" alt="" data-id=${v.courseId}>
            <div class="title">
              <h5>${v.name}</h5>
              <p>${v.desc}</p>
            </div>
            </a>
            </li>`
        })
        dom.getEle(obj).innerHTML = strHtml
    }

    dom.addEvent('.new', function(e) {
        console.log(e.target.nodeName);
        if (e.target.nodeName == 'IMG') {
            let val = e.target.dataset['id']
            location.href = 'course.html?id=' + val

        }


    })

    dom.addEvent('.course', function(e) {

        if (e.target.nodeName == 'IMG') {
            let val = e.target.dataset['id']
            location.href = 'course.html?id=' + val

        }


    })

})